package exe.jvmbytecodes;

import java.io.IOException;
import java.lang.InterruptedException;
import java.io.File;
import java.io.FilenameFilter;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;

import java.io.*;
import java.util.*;
import java.net.*;

abstract class Bytecode_ extends BytecodeViewer
{
        
        public String opcode;
        public int lineNumber;
        public String comments;
        public ArrayList<String> arguments = new ArrayList<String>();
        public char type;
        public int next;
        public String underscore;
        public String entireOpcode;
        
        public abstract int execute(Frame_ f) throws IOException;
        
        public void parse(String s)
        {
                entireOpcode = s;
                String[] s2 = s.split(":");
                entireOpcode = s2[1];
                if(s.contains(";"))
                {
                        String[] temp = s.split(";");
                        comments = temp[1];
                        temp = temp[0].split("[ \t]");
                        lineNumber = Integer.parseInt(temp[0].substring(0,temp[0].length()-1));

                        int j=1;
                        while(temp[j].compareTo("")==0)
                                j++;
                        opcode = temp[j];
                        
                                if(opcode.contains("_"))
                                {
                                        underscore = "_";
                                        String[] temp1 = opcode.split("_");
                                        opcode = temp1[0];
                                        arguments.add(temp1[1]);
                                }
                                else
                                {
                                        underscore = " ";
                                }

                                if(temp.length <= 1)
                                {
                                        ;
                                }
                                else
                                {                       
                                        for( int i = 2; i < temp.length; i++)
                                        {
                                                if(temp[i].contains(","))
                                                {
                                                        temp[i] = temp[i].substring(0, temp[i].length()-1);
                                                }
                                                arguments.add(temp[i]);
                                        }               
                                }

                                if(!comments.equals(null))
                                {
                                        String[] commentsString = comments.split(" ");
                                        for( int i = 1; i < commentsString.length; i++)
                                        {
                                                String returnType = "";
                                                if(commentsString[i].contains(":"))
                                                {
                                                        returnType = commentsString[i].substring(commentsString[i].length() - 1, commentsString[i].length());
                                                        commentsString[i] = commentsString[i].substring(0, commentsString[i].length()-1);
                                                }
                                                arguments.add(commentsString[i]);
                                                if(returnType != "")
                                                        arguments.add(returnType);
                                        }
                                }
                }
                else
                {
                        String[] temp = s.split("[ \t]");
                        lineNumber = Integer.parseInt(temp[0].substring(0,temp[0].length()-1));
                        
                        int j=1;
                        while(temp[j].compareTo("")==0)
                        j++;
                        opcode = temp[j];
                        
                        if(opcode.contains("_"))
                        {
                                underscore = "_";
                                String[] temp1 = opcode.split("_");
                                opcode = temp1[0];
                                arguments.add(temp1[1]);
                        }
                        else
                        {
                                underscore = "";
                        }

                        for(int i = 2; i < temp.length; i++)
                        {
                                if(temp[i].contains(","))
                                {
                                        temp[i] = temp[i].substring(0, temp[i].length()-1);
                                }
                                arguments.add(temp[i]);
                        }
                }
                next = lineNumber+1;
                System.out.println(opcode + " " + lineNumber + " " + arguments + " " + comments + " " + next);
        }

        public int getLineNumber()
        {
                return this.lineNumber;
        }

        public int getLocalVariableTable(String parameter)
        {
                int index = 0;
                for(int i = 0; i < gbc.classes[0].methods.get(1).localVariableTable.length; i++)
                {
                        if(gbc.classes[0].methods.get(1).localVariableTable[i][0].compareTo(parameter) == 0)
                        {
                                index = i;
                                System.out.println("The index we found is: " + index);
                                break;
                        }                       
                }
                return index;
        }

        public String toString()
        {
                return opcode + " " + arguments + " " + lineNumber;
        }
}

